

package cn.shengchao.examstar.distribute.infrastrure.service.impl;

import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.distribute.infrastrure.mapper.DistributeDeptUserRoleBindMapper;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeDeptUserRoleBind;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeDeptUserRoleBindService;
import cn.shengchao.examstar.rpc.distribute.req.UserDbIQueryPage;
import cn.shengchao.examstar.rpc.distribute.resp.UserDbIResp;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeDataStatusEnum;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeTypeEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.continew.starter.core.exception.BaseException;

import java.util.Date;
import java.util.List;

@Service
public class DistributeDeptUserRoleBindServiceImpl extends ServiceImpl<DistributeDeptUserRoleBindMapper, DistributeDeptUserRoleBind> implements DistributeDeptUserRoleBindService {

    @Override
    public Long getDeptBindIdForUser(Long userId, DistributeTypeEnum distributeTypeEnum) {
        DistributeDeptUserRoleBind distributeDeptUserRoleBind = getOne(Wrappers
            .lambdaQuery(DistributeDeptUserRoleBind.class)
            .eq(DistributeDeptUserRoleBind::getUserId, userId)
            .eq(DistributeDeptUserRoleBind::getDistributeType, distributeTypeEnum.getType())
            .eq(DistributeDeptUserRoleBind::getDataStatus, DistributeDataStatusEnum.STATUS_CURT.getStatus()));
        return distributeDeptUserRoleBind == null ? null : distributeDeptUserRoleBind.getDeptId();
    }
    @Transactional
    @Override
    public void disableDeptBind(Long userId, DistributeTypeEnum distributeTypeEnum) {
        DistributeDeptUserRoleBind distributeDeptUserRoleBind = getOne(Wrappers
                .lambdaQuery(DistributeDeptUserRoleBind.class)
                .eq(DistributeDeptUserRoleBind::getUserId, userId)
                .eq(DistributeDeptUserRoleBind::getDistributeType, distributeTypeEnum.getType())
                .eq(DistributeDeptUserRoleBind::getDataStatus, DistributeDataStatusEnum.STATUS_CURT.getStatus()));
        if (distributeDeptUserRoleBind != null) {
            distributeDeptUserRoleBind.setDataStatus(DistributeDataStatusEnum.STATUS_HIS.getStatus());
            distributeDeptUserRoleBind.setValidEndTime(new Date());
            updateById(distributeDeptUserRoleBind);
        }else {
            throw new BaseException("用户原有的部门绑定不存在");
        }
    }

    @Transactional
    @Override
    public void processUserDeptRole(DistributeTypeEnum distributeTypeEnum,
                                    Long userId,
                                    Long deptId,
                                    String sysRoleCode,
                                    String distributeRoleCode) {
        //销售或内容部门的只能有一个内置部门
        update(Wrappers.lambdaUpdate(DistributeDeptUserRoleBind.class)
            .eq(DistributeDeptUserRoleBind::getUserId, userId)
            .eq(DistributeDeptUserRoleBind::getDistributeType, distributeTypeEnum.getType())
            .eq(DistributeDeptUserRoleBind::getDataStatus, DistributeDataStatusEnum.STATUS_CURT.getStatus())
            .set(DistributeDeptUserRoleBind::getDataStatus, DistributeDataStatusEnum.STATUS_HIS.getStatus())
            .set(DistributeDeptUserRoleBind::getValidEndTime, new Date()));
        DistributeDeptUserRoleBind distributeDeptUserRoleBind = new DistributeDeptUserRoleBind();
        distributeDeptUserRoleBind.setUserId(userId);
        distributeDeptUserRoleBind.setDistributeType(distributeTypeEnum.getType());
        distributeDeptUserRoleBind.setDeptId(deptId);
        distributeDeptUserRoleBind.setDataStatus(DistributeDataStatusEnum.STATUS_CURT.getStatus());
        distributeDeptUserRoleBind.setDistributeRoleCode(distributeRoleCode);
        distributeDeptUserRoleBind.setSysRoleCode(sysRoleCode);
        distributeDeptUserRoleBind.setValidStartTime(new Date());
        save(distributeDeptUserRoleBind);
    }

    @Override
    public PageResult<UserDbIResp> deptUserPageQuery(UserDbIQueryPage userDbIQueryPage) {
        Page<Object> page = PageHelper.startPage(userDbIQueryPage.getPage(), userDbIQueryPage.getSize());
        List<UserDbIResp> UserDbIList = baseMapper
                .selectDeptUserPageQuery(userDbIQueryPage);
        return PageResult.of(page.getTotal(), UserDbIList);
    }


}
